package cn.schoolwow.quickdao.test.module.business.user.dml.user.flow.edit.update;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.domain.database.dml.UpdateType;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

import java.util.List;

public class UpdateUserByUniqueKeyFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        singleUpdate(flowContext);
        multipleUpdate(flowContext);
    }

    @Override
    public String name() {
        return "根据用户唯一性约束更新用户";
    }

    private void singleUpdate(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = (User) flowContext.checkData("insertUser");

        insertUser.setPassword("222222");
        int effect = dao.updateType(UpdateType.UpdateByUniqueKey).update(insertUser);
        Assert.assertEquals(1, effect);
    }

    private void multipleUpdate(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        List<User> insertUserList = (List<User>) flowContext.checkData("insertUserList");

        for(User user:insertUserList){
            user.setPassword("665544");
        }
        int effect = dao.updateType(UpdateType.UpdateByUniqueKey).update(insertUserList);
        Assert.assertEquals(insertUserList.size(), effect);
    }
}
